﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace project
{
    public partial class GarageManage : Form
    {
        public GarageManage()
        {
            InitializeComponent();
        }

        private void GarageManage_Load(object sender, EventArgs e)
        {
            InitData();
        }
        public void InitData()
        {
            string sql = "SELECT * FROM [dbo].[Garage]";
            DataTable dt= DBHelper.GetTable(sql);
            this.dataGridView1.DataSource = dt;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            string id = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            string sql = "SELECT * FROM [dbo].[Garage] g join CarType c on g.CarTypeId=c.CarTypeId where Id="+ id;
            DataTable dt = DBHelper.GetTable(sql);
            foreach (DataRow item in dt.Rows)
            {
                this.textBoxNumber.Text = item["Number"].ToString();
                this.textBoxNumbering.Text = item["CarNumbering"].ToString();
                this.textBoxColor.Text = item["Color"].ToString();
                this.textBoxInPrice.Text = item["InStoreagePrice"].ToString();
                this.textBoxOutPrice.Text = item["OutPrice"].ToString();
                this.textBoxName.Text = item["CarName"].ToString();
            }
            
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {

            string numbering = this.textBoxNumbering.Text, InPrice = this.textBoxInPrice.Text, outPrice = this.textBoxOutPrice.Text, number = this.textBoxNumber.Text, color = this.textBoxColor.Text, name = this.textBoxName.Text, Id = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            string sql = "UPDATE [dbo].[Garage]SET [CarName] ='{0}' ,[InStoreagePrice] ='{1}' ,[OutPrice] = '{2}',[CarNumbering] = '{3}',[Color] ='{4}' ,[Number] = {5} WHERE Id={6}";
            sql = string.Format(sql,name,InPrice,outPrice,numbering,color,number,Id);
            int re= DBHelper.ExecuteNonQuery(sql    );
            if (re>0)
            {
                MessageBox.Show("修改成功");
                InitData();

            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {

        }

        private void buttonClose_Click(object sender, EventArgs e)
        {

        }
    }
}
